
var attachment = {

add_count : 0,
//formats : '.doc.xls.ppt.pdf.rar.zip.bmp.jpeg.jpg.gif',
formats : '.bat.exe',

//增加输入表单
//参数parentId:插入表单所在元素的ID；key:文件表单的标识符(也可理解为附件在表中的字段名,用于为不同的字段添加附件)
addMore: function(parentId,key){
	var fileList = document.getElementById(parentId);
	var div = document.createElement('div');
	var input = document.createElement('input');
	input.type = 'file';
	input.name = key + this.add_count;
	input.size = '30';
	input.style.height = '20px';
	input.style.width = '345px';
	input.onkeydown = function() { return false; };
	input.onkeyup = function() { return false; };
	input.oncontextmenu = function() { return false; };
	div.appendChild(input);
	var button = document.createElement('input');
	button.type = 'button';
	button.value = '删除附件'
	button.style.height = '20px';
	button.onclick = function () { this.parentNode.parentNode.removeChild(this.parentNode); };
	div.appendChild(button);
	fileList.appendChild(div);
	this.add_count += 1;
},

//验证文件类型是否合法
validate : function(){
	var result=true;
	var elements = document.getElementsByTagName('input');
	for (var i = elements.length - 1; i >= 0; i--) {
		if (elements[i].type == 'file') {
			// 判断输入框中是否有文件
			if (elements[i].value == ''){
				elements[i].parentNode.parentNode.removeChild(elements[i].parentNode);
			}else{
				//判断文件后缀是否合法
				var val = elements[i].value.toLowerCase();
				for (var j = 0; j < this.formats.length; j++) {
					var exts = val.split('.');
					var extname = '.'+exts[exts.length-1];
					if(this.formats.indexOf(extname)!=-1){
						//文件后缀不合法
						result=false;
						alert('只能导入 *.doc,*.xls,*.ppt,*.pdf,*.rar,*.zip,*.bmp,*.jpeg,*.jpg,*.gif 格式文件!');
						return result;
					}
				}
			}
		}
	}
	return result;
},

//删除附件
//参数：this,类名,字段名,记录ID,附件路径
deleteLine : function(comp, attachmentno){
	var url = 'attachmentDelete.do?attachmentno='+attachmentno ;
	var url2 = encodeURI(url);

	$.ajax({
		url: encodeURI(url2),
		method: "post",
		complete: function(request) {
			var status =request.responseXML.getElementsByTagName('value')[0].childNodes[0].nodeValue;
			//状态为 200 表示成功
			if (status == 200) {
				var table = comp.parentNode.parentNode.parentNode;
				table.removeChild(comp.parentNode.parentNode);
				if (table.rows.length == 2) {
					table.removeChild(table.rows[0]);
				}
			}
		}
	});

},

//下载附件
download : function(attachmentno){
	var url = 'attachmentDownload.do?attachmentno='+attachmentno;
	var url2 = encodeURI(url);
	window.location.href=encodeURI(url2); 
}

}